<template>
  <div class="message-list">
    <div
      v-for="message in messages"
      :class="['message', message.role]"
    >
      <div class="avatar">
        <img
          v-if="message.role === 'assistant'"
          src="@/assets/images/profile.jpg"
          alt="AI"
        >
        <img
          v-else
          src="@/assets/images/profile.jpg"
          alt="User"
        >
      </div>
      <div class="content">
        <div class="text" v-html="formatMessage(message.content)"></div>
        <div class="timestamp">{{ formatTime(message.createTime) }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'MessageList',
  props: {
    messages: {
      type: Array,
      required: true
    }
  },
  methods: {
    formatMessage(text) {
      // 简单的Markdown转HTML（实际应用中应该使用专门的库）
      return text
        .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
        .replace(/\*(.*?)\*/g, '<em>$1</em>')
        .replace(/`(.*?)`/g, '<code>$1</code>')
        .replace(/\n/g, '<br>')
    },
    formatTime(timestamp) {
      return new Date(timestamp).toLocaleString()
    }
  }
}
</script>

<style scoped>
.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

.message {
  display: flex;
  margin-bottom: 20px;
  max-width: 80%;
}

.message.user {
  margin-left: auto;
  flex-direction: row-reverse;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 15px;
}

.message.user .avatar {
  margin-right: 0;
  margin-left: 15px;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.content {
  flex: 1;
}

.message.ai .content {
  background-color: #f5f5f5;
  border-radius: 0 10px 10px 10px;
  padding: 10px 15px;
}

.message.user .content {
  background-color: #e3f2fd;
  border-radius: 10px 0 10px 10px;
  padding: 10px 15px;
}

.timestamp {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

.message.user .timestamp {
  text-align: right;
}
</style>
